/*
 * Copyright (c) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.sample.gan;

/**
 * Parses parameters for the Google Affiliate Network Sample Code
 * @author namos@google.com (Ryan Oman)
 */
public class Params {
  public static enum Api {CC_OFFERS, EVENTS, PUBLISHER, PUBLISHERS, ADVERTISER, ADVERTISERS};

  private final Api type;
  private final String role;
  private final String roleId;
  private final String lookupId;

  public Params(Api type, String roleId, String role, String lookupId) {
    this.type = type;
    this.role = role;
    this.roleId = roleId;
    this.lookupId = lookupId;
  }

  public Api getType() {
    return this.type;
  }

  public boolean hasRole() {
    return this.role != null;
  }

  public String getRole() {
    return this.role;
  }

  public boolean hasRoleId() {
    return this.roleId != null;
  }

  public String getRoleId() {
    return this.roleId;
  }

  public boolean hasLookupId() {
    return this.lookupId != null;
  }

  public String getLookupId() {
    return this.lookupId;
  }

  public static Params parse(String[] args) {
    if (args.length < 1) {
      return null;
    }

    String api = args[0].toLowerCase();
    Api type = null;
    String role = null;
    String roleId = null;
    String lookupId = null;

    if (api.equals("ccoffers")) {
      type = Api.CC_OFFERS;

      if (args.length == 2) {
        roleId = args[1];
      } else {
        return null;
      }
    } else if (api.equals("events") || api.equals("advertisers") || api.equals("publishers")) {
      if (api.equals("events")) {
        type = Api.EVENTS;
      } else if (api.equals("advertisers")) {
        type = Api.ADVERTISERS;
      } else { // api.equals("publsihers")
        type = Api.PUBLISHERS;
      }

      if (args.length == 3) {
        role = args[1];
        roleId = args[2];
      } else {
        return null;
      }
    } else if (api.equals("advertiser") || api.equals("publisher")) {
      if (api.equals("advertiser")) {
        type = Api.ADVERTISER;
      } else { // api.equals("publisher")
        type = Api.PUBLISHER;
      }

      if (args.length == 3) {
        role = args[1];
        roleId = args[2];
      } else if (args.length == 4) {
        role = args[1];
        roleId = args[2];
        lookupId = args[3];
      } else {
        return null;
      }
    } else {
      return null;
    }

    return new Params(type, roleId, role, lookupId);
  }

  public static void printUsage() {
    printUsage("");
  }

  public static void printUsage(String api) {
    if (api.equals("ccoffer")) {
      System.out.println("  Credit Card Offers Sample Usage:");
      System.out.println("    Usage (Publishers only]: ccoffers [your role ID]");
      System.out.println();
    } else if (api.equals("event")) {
      System.out.println("  Event Api Sample Usage:");
      System.out.println("    Usage: events [your role={publishers|advertisers}] [your role ID]");
      System.out.println();
    } else if (api.equals("advertiser")) {
      System.out.println("  Advertiser Api Sample Usage:");
      System.out.println("    List Usage: advertisers [your role={publishers|advertisers}]"
                         + "[your role ID]");
      System.out.println("    Get Usage: advertiser [your role={publishers|advertisers}]"
                         + "[your role ID] [lookup ID (exclude if looking up self)]");
      System.out.println("    Example: You want to look up yourself (you are an advertiser):"
                         + "\"advertiser advertisers 1234\"");
      System.out.println("    Example: You want to look up advertiser 1234 (you are a publisher):"
                         + "\"advertiser publishers 5678 1234\"");
      System.out.println();
    } else if (api.equals("publisher")) {
      System.out.println("  Publisher Api Sample Usage:");
      System.out.println("    List Usage: publishers [your role={publishers|advertisers}]"
                         + "[your role ID]");
      System.out.println("    Get Usage: publisher [your role={publishers|advertisers}]"
                         + "[your role ID] [lookup ID (exclude if looking up self)]");
      System.out.println("    Example: You want to look up yourself (you are a publisher):"
                         + "\"publisher publishers 1234\"");
      System.out.println("    Example: You want to look up publisher 1234 (you are an advertiser):"
                         + "\"publisher advertisers 5678 1234\"");
      System.out.println();
    } else {
      System.out.println("Sample Code Usage: ");
      printUsage("ccoffer");
      printUsage("event");
      printUsage("advertiser");
      printUsage("publisher");
    }
  }
}
